/** @file    GadgetCommunicationBox.cpp
 *  @author  Alessandro Polo
 *  @version $Id: GadgetCommunicationBox.cpp 2748 2010-07-11 22:07:06Z alex $
 *  @brief
 * File containing methods for the wosh::application::GadgetCommunicationBox class.
 * The header for this class can be found in GadgetCommunicationBox.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include "GadgetCommunicationBox.h"
 #include "ui_GadgetCommunicationBox.h"

 #include <interfaces/services/CommunicationManager.h>
 #include <framework/gui/GuiUtilities.h>
 #include <core/List.h>
 #include <QtGui/QMessageBox>


namespace wosh {
  namespace gui {

 WOSH_REGISTER(wosh::gui::GadgetCommunicationBox, "wosh::gui::KioskGadgetGeneric", 1.0, _static_GadgetCommunicationBox )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

GadgetCommunicationBox::GadgetCommunicationBox() : KioskGadgetGeneric()
 {
	//QWidget* widget = new QWidget(this);
//	this->verticalLayout->addWidget(widget);
	this->m_ui = new Ui::GadgetCommunicationBox();
	this->m_ui->setupUi(this);

//	Object::setName("GadgetCommunicationBox");
//	Object::setType(_GadgetCommunicationBox_TYPE);
//	KioskGadgetGeneric::setTitle(tr("Communication Box"));

	this->setWindowTitle( tr("Communication Box") );
	this->getWidgetURI().setName( Utilities::format("GadgetCommunicationBox#%10lu", Utilities::randomLong()) );
	this->getTargetURI().fromString( "*:Communication" );

	this->m_ui->tableView->setMethodList(_CommunicationManager_METHOD_messages_my);
	this->m_ui->tableView->setResizeStretch(2);
	connect(this->m_ui->tableView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(tableView_doubleClicked(const QModelIndex&)));


	connect(this->m_ui->btnOpen, SIGNAL(clicked()), this, SLOT(btnOpen_clicked()));
	connect(this->m_ui->btnMarkSeen, SIGNAL(clicked()), this, SLOT(btnMarkSeen_clicked()));

	connect(this->m_ui->btnRefresh, SIGNAL(clicked()), this, SLOT(refreshAll()));
 }

GadgetCommunicationBox::~GadgetCommunicationBox()
 {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void GadgetCommunicationBox::setTargetURI( const URI& uri ) {
	IWidget::setTargetURI(uri);
	if ( uri.isEmpty() || !uri.isValid() )
		IWidget::setTargetURI( URI("*:Communication") );
 }

void GadgetCommunicationBox::refreshAll() {
	postMessageForTarget( new Message(new Request(_CommunicationManager_METHOD_messages_my)) );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void GadgetCommunicationBox::tableView_doubleClicked(const QModelIndex&) {
	btnOpen_clicked();
 }

void GadgetCommunicationBox::btnOpen_clicked() {
	QMessageBox::information(this, "TODO", "should open message" );
}

void GadgetCommunicationBox::btnMarkSeen_clicked() {
	Variant dataID = this->m_ui->tableView->getCurrentItemAsVariantAtCol(-1);
	List* dl = new List();
	dl->push_back( dataID.clone() );
	dl->push_back<Variant>( "READ" );
	postMessageForTarget( new Message(new Request(_CommunicationManager_METHOD_mark, dl)) );
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void GadgetCommunicationBox::busMessage( const Message& message, const Bus* )
 {
	if ( message.isEmpty() ) return;
	if ( !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) )
		return;
	const wosh::Response* response = message.getContent()->asResponse();
	this->m_ui->tableView->evalResponse(response);
 }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace gui
}; // namespace wosh
